package com.cjjd.c.feature.mine.setting;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cjjd.c.EventBusKeys;
import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarFragment;
import com.cjjd.c.bean.CustomInfo;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.MobileCode;
import com.cjjd.c.feature.dialog.ChangeNameDialog;
import com.cjjd.c.feature.mine.feedback.FeedBackActivity;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.ImageUtil;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/8/25.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class SettingFragment extends ActionBarFragment {
    @Bind(R.id.iv_logo)
    ImageView ivLogo;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_sex)
    TextView tvSex;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.tv_email)
    TextView tvEmail;
    @Bind(R.id.tv_clear)
    TextView tvClear;
//    @Bind(R.id.tb_save_flow)
//    ToggleButton tbSaveFlow;
//    @Bind(R.id.tb_push_message)
//    ToggleButton tbPushMessage;

    @Override
    public Object getTitleString() {
        return "设置";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_setting;
    }


    @Override
    public void onResume() {
        super.onResume();
        tvClear.setText("清除缓存  ("+ImageUtil.getFileSize(mContext)+")");
    }



    public void initView() {
        sexList.add("女");
        sexList.add("男");


        getCustomerInfo();
    }

    private void getCustomerInfo() {
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));

        Call<HttpResult<CustomInfo>> call = service.getCustomerInfo(JsonUtils.convertParam("getCustomerInfo", h1));
        call.enqueue(new CallbackImpl<CustomInfo>() {
            @Override
            public void responseSuccess(Call<HttpResult<CustomInfo>> call, Response<HttpResult<CustomInfo>> response) {
                ChangeUI(response.body().getData().customerInfo);
            }
        });
    }

    private void ChangeUI(CustomInfo.CustomerInfoBean customerInfo) {

        if (customerInfo == null) {
            return;
        }

//        Glide.with(this).load(customerInfo.image).into(ivLogo);
        ImageUtil.displayRoundImage(customerInfo.image, ivLogo, 200);
        tvName.setText(customerInfo.nickName);
        if ("1".equals(customerInfo.sex)) {
            tvSex.setText("男");
        } else {
            tvSex.setText("女");
        }
        tvPhone.setText(customerInfo.mobile);
        tvEmail.setText(customerInfo.email);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.rl_head_image, R.id.tv_name, R.id.tv_sex, R.id.tv_phone, R.id.tv_email, R.id.btn_login_out, R.id.tv_clear, R.id.yijianfankui, R.id.guanyu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_head_image:
                setHeadImage();
                break;
            case R.id.tv_name:
                setNikeName();
                break;
            case R.id.tv_sex:
                showChooseDateDialog(sexList, "选择性别");
                break;
            case R.id.tv_phone:
                //setPhone();
                break;
            case R.id.tv_email:
                //setEmail();
                break;
            case R.id.btn_login_out:
                logOut();
                break;
            case R.id.tv_clear:
                ImageUtil.clearDiscCache();
                ToastUtil.showToast("缓存清理成功");
                tvClear.setText("清除缓存  (0K)");

                break;
            case R.id.yijianfankui:
                //意见反馈
                FeedBackActivity.lauch(mContext);
                break;
            case R.id.guanyu:
                //客服中心

                break;
        }
    }

    private void logOut() {
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));

        Call<HttpResult<String>> call = service.logout(JsonUtils.convertParam("logout", h1));
        call.enqueue(new CallbackImpl<String>() {
            @Override
            public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                ToastUtil.showLongToast(mContext, "成功退出登录");
                SharePrefUtil.clear(mContext);
                EventBus.getDefault().post(EventBusKeys.LOGOUT);
                mContext.onBackPressed();
            }
        });
    }

    /**
     * 设置头像信息
     */
    private void setHeadImage() {
    }

    /**
     * 设置邮箱
     */
    private void setEmail() {

        if (PhoneStep == 0) {
            HashMap<String, Object> h1 = new HashMap<>();
            h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
            h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
            h1.put("mobile", "15500000005");
            Call<HttpResult<MobileCode>> updateCustomerInfo = service.getBindMobileCode(JsonUtils.convertParam("getBindMobileCode", h1));
            updateCustomerInfo.enqueue(new CallbackImpl<MobileCode>() {
                @Override
                public void responseSuccess(Call<HttpResult<MobileCode>> call, Response<HttpResult<MobileCode>> response) {
                    PhoneStep++;
                    phoneCode = response.body().getData().captcha;
                    sessionId = response.body().getData().sessionId;
                }
            });
        } else if (PhoneStep == 1) {
            HashMap<String, Object> h1 = new HashMap<>();
            h1.put("sessionId", sessionId);
            h1.put("mobile", "15500000005");
            h1.put("captcha", phoneCode);
            Call<HttpResult<String>> checkBindMobileCode = service.checkBindMobileCode(JsonUtils.convertParam("checkBindMobileCode", h1));
            checkBindMobileCode.enqueue(new CallbackImpl<String>() {
                @Override
                public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                    PhoneStep++;
                }
            });
        } else if (PhoneStep == 2) {
            HashMap<String, Object> h1 = new HashMap<>();
            h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
            h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
            h1.put("mobile", "155000000015");
            Call<HttpResult<MobileCode>> getBindNewMobileCode = service.getBindNewMobileCode(JsonUtils.convertParam("getBindNewMobileCode", h1));
            getBindNewMobileCode.enqueue(new CallbackImpl<MobileCode>() {
                @Override
                public void responseSuccess(Call<HttpResult<MobileCode>> call, Response<HttpResult<MobileCode>> response) {
                    PhoneStep++;
                    newPhoneCode = response.body().getData().captcha;
                    newSessionId = response.body().getData().sessionId;
                }
            });
        } else if (PhoneStep == 3) {
            HashMap<String, Object> h1 = new HashMap<>();
            h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
            h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
            h1.put("captchaSessionId", newSessionId);
            h1.put("mobile", "15500000005");
            h1.put("newMobile", "15500000015");
            h1.put("captcha", newPhoneCode);
            Call<HttpResult<String>> saveBindMobile = service.saveBindMobile(JsonUtils.convertParam("saveBindMobile", h1));
            saveBindMobile.enqueue(new CallbackImpl<String>() {
                @Override
                public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                    PhoneStep++;
                }
            });
        }

    }

    private int PhoneStep = 0;
    private String phoneCode = "";
    private String sessionId = "";
    private String newPhoneCode = "";
    private String newSessionId = "";

    /**
     * 设置手机号
     */
    private void setPhone() {

        if (PhoneStep == 0) {
            HashMap<String, Object> h1 = new HashMap<>();
            h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
            h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
            h1.put("mobile", "13566668888");
            Call<HttpResult<MobileCode>> updateCustomerInfo = service.getBindMobileCode(JsonUtils.convertParam("getBindMobileCode", h1));
            updateCustomerInfo.enqueue(new CallbackImpl<MobileCode>() {
                @Override
                public void responseSuccess(Call<HttpResult<MobileCode>> call, Response<HttpResult<MobileCode>> response) {
                    PhoneStep++;
                    phoneCode = response.body().getData().captcha;
                    sessionId = response.body().getData().sessionId;
                }
            });
        } else if (PhoneStep == 1) {
            HashMap<String, Object> h1 = new HashMap<>();
            h1.put("sessionId", sessionId);
            h1.put("mobile", "13566668888");
            h1.put("captcha", phoneCode);
            Call<HttpResult<String>> checkBindMobileCode = service.checkBindMobileCode(JsonUtils.convertParam("checkBindMobileCode", h1));
            checkBindMobileCode.enqueue(new CallbackImpl<String>() {
                @Override
                public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                    PhoneStep++;
                }
            });
        } else if (PhoneStep == 2) {
            HashMap<String, Object> h1 = new HashMap<>();
            h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
            h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
            h1.put("mobile", "13566668888");
            Call<HttpResult<MobileCode>> getBindNewMobileCode = service.getBindNewMobileCode(JsonUtils.convertParam("getBindNewMobileCode", h1));
            getBindNewMobileCode.enqueue(new CallbackImpl<MobileCode>() {
                @Override
                public void responseSuccess(Call<HttpResult<MobileCode>> call, Response<HttpResult<MobileCode>> response) {
                    PhoneStep++;
                    newPhoneCode = response.body().getData().captcha;
                    newSessionId = response.body().getData().sessionId;
                }
            });
        } else if (PhoneStep == 3) {
            HashMap<String, Object> h1 = new HashMap<>();
            h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
            h1.put("sessionId", newSessionId);
            h1.put("mobile", "13566668888");
            h1.put("newMobile", "15500000009");
            h1.put("captcha", newPhoneCode);
            Call<HttpResult<String>> saveBindMobile = service.saveBindMobile(JsonUtils.convertParam("saveBindMobile", h1));
            saveBindMobile.enqueue(new CallbackImpl<String>() {
                @Override
                public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                    PhoneStep++;
                }
            });
        }

    }


    private ArrayList<String> sexList = new ArrayList<>();

    @Override
    protected void getChooseDialog(int index, String message, String title) {
        setSex(index);
    }

    /**
     * 设置性别 0女  1男
     */
    private void setSex(int sex) {
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("gender", "" + sex);
        Call<HttpResult<String>> updateCustomerInfo = service.updateCustomerInfo(JsonUtils.convertParam("updateCustomerInfo", h1));
        updateCustomerInfo.enqueue(new CallbackImpl<String>() {
            @Override
            public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                ToastUtil.showLongToast(mContext, "成功设置性别");
                getCustomerInfo();
            }

        });
    }

    /**
     * 设置昵称
     */
    private void setNikeName() {

        new ChangeNameDialog(mContext, "设置名字", "请输入名字", new ChangeNameDialog.OnClickListener() {
            @Override
            public void onClick(String inputString) {

                if (TextUtils.isEmpty(inputString)) {
                    ToastUtil.showLongToast(mContext, "请输入正确名字");
                    return;
                }

                HashMap<String, Object> h1 = new HashMap<>();
                h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
                h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
                h1.put("nickName", inputString);
                Call<HttpResult<String>> updateCustomerInfo = service.updateCustomerInfo(JsonUtils.convertParam("updateCustomerInfo", h1));
                updateCustomerInfo.enqueue(new CallbackImpl<String>() {
                    @Override
                    public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                        getCustomerInfo();
                        ToastUtil.showLongToast(mContext, "成功设置昵称");
                    }
                });
            }
        }).show();

    }


    EditText et_money;
}
